<?php 
class BaseDao{
	var $table_name;
	var $primary_key = 'id';
	var $db;
	
	function __construct($db,$table_name='',$primary_key = 'id'){
		$this->db = $db;
		$this->table_name = $table_name;
		$this->primary_key = $primary_key;
	}
	
	/**
	 * 淇濆瓨
	 * @param  $sets 閿�鏁扮粍
	 */
    public function save($sets){
    	if($sets != null && count($sets) > 0){
    		$sql = 'INSERT INTO '.$this->table_name;
    		$sql =$sql. ' ('.implode(',',array_keys($sets)).') VALUES ';
    		$sql =$sql.'('.substr(str_repeat(",?",count($sets)),1).')';
    		$stm = $this->db->prepare($sql);
    		$result = $stm->execute(array_values($sets));
    		$stm = null;
    		if($result){
    			$id = $this->db->lastInsertId();
    			$sets['new_id'] = intval($id);
    			return $sets;
    		}
    	}
    	return null;
    }
    
    /**
     * 鏍规嵁鏉′欢鍒犻櫎璁板綍
     * @param  $whereSql
     * @param  $whereValues
     */
    public function delete($whereSql,$whereValues) {
    	$sql = 'DELETE FROM '.$this->table_name;
    	$stm = null;
    	$result = false;
    	if($whereSql != null && trim($whereSql) != ''){
    		$sql =$sql. ' WHERE '.$whereSql;
    		$stm = $this->db->prepare($sql);
    		$result = $stm->execute($whereValues);
    	}else{
    		$stm = $this->db->prepare($sql);
    		$result = $stm->execute();
    	}
    	$stm = null;
    	return $result;
    }
    
    /**
     * 鏍规嵁鏉′欢鏇存柊璁板綍
     * @param  $values  鏇存柊瀛楁鐨勯敭鍊兼暟缁�
     * @param  $whereSql
     * @param  $whereValues
     */
    public function update($values,$whereSql,$whereValues){
    	if($values != null && count($values) > 0){
    		$arr = array_values($values);
    		$sql = 'UPDATE '.$this->table_name.' SET ';
    		$sql =$sql. ' '.implode('=?,',array_keys($values)).'=? ';
    		if($whereSql != null && trim($whereSql) != ''){
    			$sql =$sql. ' WHERE '.$whereSql;
    			$arr = array_merge($arr,$whereValues);
    		}
    		$stm = $this->db->prepare($sql);
    		$result = $stm->execute($arr);
    		$stm = null;
    	    return $result;
    	}
    	return false;
    }
    
    /**
     *鏍规嵁Id鏌ヨ璁板綍
     */
    public function getById($key,$field = '*'){
    	$sql = 'SELECT '.$field.' FROM '.$this->table_name.' WHERE '.$this->primary_key.'=? LIMIT 0,1';
    	$stm = $this->db->prepare($sql);
    	$num = $stm->execute(array($key));
    	$result = $stm->fetchAll(PDO::FETCH_ASSOC);
    	$stm = null;
    	if($result != null && count($result)>0){
    		$result = $result[0];
    	}
    	return $result;
    }
    
    /**
     *鏌ヨ婊¤冻鏉′欢鐨勪竴鏉¤褰�
     */
    public function getOne($whereSql,$whereValues,$orderbys = array(),$field = '*'){
    	$sql = 'SELECT '.$field.' FROM '.$this->table_name;
    	$result = null;
    	$stm = null;
    	$orderbySql = '';
    	if($orderbys != null && count($orderbys) > 0){
    		$orderbySql =' ORDER BY ' . implode(',',$orderbys);
    	}
    	if($whereSql != null && trim($whereSql) != ''){
    		$sql =$sql. ' WHERE '.$whereSql . $orderbySql .' LIMIT 0,1';
    		$stm = $this->db->prepare($sql);
    		if($stm->execute($whereValues)){
    			$result  = $stm->fetchAll(PDO::FETCH_ASSOC);
    		}
    	}else{
    		$sql =$sql.$orderbySql.' LIMIT 0,1';
    		$stm = $this->db->prepare($sql);
    		if($stm->execute()){
    			$result = $stm->fetchAll(PDO::FETCH_ASSOC);
    		}
    	}
    	$stm = null;
    	if($result != null && count($result) > 0){
    		$result = $result[0];
    	}
    	return $result;
    }
    
    /**
     *鏌ヨ婊¤冻鏉′欢鐨勬墍鏈夎褰�
     */
    public function getAll($whereSql,$whereValues,$orderbys = array(),$field = '*'){
    	$sql = 'SELECT '.$field.' FROM '.$this->table_name;
    	$result = null;
    	$stm = null;
    	$orderbySql = '';
    	if($orderbys != null && count($orderbys) > 0){
    		$orderbySql =' ORDER BY ' . implode(',',$orderbys);
    	}
    	if($whereSql != null && trim($whereSql) != ''){
    		$sql =$sql. ' WHERE '.$whereSql.$orderbySql;
    		$stm = $this->db->prepare($sql);
    		if($stm->execute($whereValues)){
    			$result  = $stm->fetchAll(PDO::FETCH_ASSOC);
    		}
    	}else{
    		$sql =$sql.$orderbySql;
    		$stm = $this->db->prepare($sql);
    		if($stm->execute()){
    			$result = $stm->fetchAll(PDO::FETCH_ASSOC);
    		}
    	}
    	$stm = null;
    	return $result;
    }
    
    
    

    /**
     *鏌ヨ婊¤冻鏉′欢鐨勬墍鏈夎褰�
     */
    public function getAll2Array($whereSql,$whereValues,$orderbys = array(),$field = '*'){
    	$sql = 'SELECT '.$field.' FROM '.$this->table_name;
    	$result = null;
    	$stm = null;
    	$orderbySql = '';
    	if($orderbys != null && count($orderbys) > 0){
    		$orderbySql =' ORDER BY ' . implode(',',$orderbys);
    	}
    	if($whereSql != null && trim($whereSql) != ''){
    		$sql =$sql. ' WHERE '.$whereSql.$orderbySql;
    		$stm = $this->db->prepare($sql);
    		if($stm->execute($whereValues)){
    			$result  = $stm->fetchAll(PDO::FETCH_NUM);
    		}
    	}else{
    		$sql =$sql.$orderbySql;
    		$stm = $this->db->prepare($sql);
    		if($stm->execute()){
    			$result = $stm->fetchAll(PDO::FETCH_NUM);
    		}
    	}
    	$stm = null;
    	return $result;
    }
    
    
    
    /**
     * 鏌ヨ鍒嗛〉璁板綍
     * @param  $whereSql
     * @param  $whereValues
     * @param  $firstindex
     * @param  $maxresult
     * @param  $orderbys
     * @param  $field
     */
    public function getScrollData($whereSql,$whereValues,$firstindex,$maxresult,$orderbys = array(),$field = '*'){
    	$sql = 'SELECT '.$field.' FROM '.$this->table_name;
    	$result = null;
    	$stm = null;
    	$orderbySql = '';
    	if($orderbys != null && count($orderbys) > 0){
    		$orderbySql =' ORDER BY ' . implode(',',$orderbys);
    	}
    	if($whereSql != null && trim($whereSql) != ''){
    		$sql =$sql. ' WHERE '.$whereSql.$orderbySql.' LIMIT '.$firstindex.','.$maxresult;
    		$stm = $this->db->prepare($sql);
    		if($stm->execute($whereValues)){
    			$result  = $stm->fetchAll(PDO::FETCH_ASSOC);
    		}
    	}else{
    		$sql =$sql.$orderbySql.' LIMIT '.$firstindex.','.$maxresult;
    		$stm = $this->db->prepare($sql);
    		if($stm->execute()){
    			$result = $stm->fetchAll(PDO::FETCH_ASSOC);
    		}
    	}
    	$stm = null;
    	return $result;
    }
    
    /**
     * 鏍规嵁鏉′欢鏌ヨ璁板綍鏁�
     * @param $whereSql
     * @param $whereValues
     */
    public function getCount($whereSql,$whereValues){
    	$sql = 'SELECT count(1) FROM '.$this->table_name;
    	$result = 0;
    	$stm = null;
    	if($whereSql != null && trim($whereSql) != ''){
    		$sql =$sql. ' WHERE '.$whereSql;
    		$stm = $this->db->prepare($sql);
    		if($stm->execute($whereValues)){
    			$result  = $stm->fetchColumn();
    		}
    	}else{
    		$stm = $this->db->prepare($sql);
    		if($stm->execute()){
    			$result = $stm->fetchColumn();
    		}
    	}
    	$stm = null;
    	return $result;
    }
    
    
    
    /**
     *
     */
    public function getBySql($sql,$whereValues=array()){
    	if(isset($whereValues) && count($whereValues) > 0){
    		$stm = $this->db->prepare($sql);
    		if($stm->execute($whereValues)){
    			$result  = $stm->fetchAll(PDO::FETCH_ASSOC);
    		}
    	}else{
    		$stm = $this->db->prepare($sql);
    		if($stm->execute()){
    			$result = $stm->fetchAll(PDO::FETCH_ASSOC);
    		}
    	}
    	$stm = null;
    	return $result;
    }
    
    
}  

?>